from django.db import models
from django.utils import timezone


class Station(models.Model):
    name = models.CharField('站点名称', max_length=100)
    did = models.CharField('站点DID', max_length=20, unique=True)
    price_version = models.CharField('价格版本', max_length=50, blank=True, null=True)
    host = models.CharField('服务器地址', max_length=100)
    port = models.PositiveIntegerField('端口号')
    created_at = models.DateTimeField('添加时间', default=timezone.now)

    class Meta:
        verbose_name = '站点管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Pile(models.Model):
    number = models.CharField('桩号', max_length=20, unique=True)
    gun_count = models.PositiveIntegerField('枪数量', default=2)
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属站点')
    logged_in = models.BooleanField('登录状态', default=False)
    vin = models.CharField('VIN码', max_length=17, blank=True, null=True)
    created_at = models.DateTimeField('添加时间', default=timezone.now)

    class Meta:
        verbose_name = '充电桩管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number

    def connection_info(self):
        return {
            'server_ip': self.station.host,
            'server_port': self.station.port,
            'pile_id': self.number,
            'gun_count': str(self.gun_count).zfill(2),
            'vin': self.vin
        }
    def create_gun_statuses(self):
        """根据枪数量自动创建枪状态"""
        for i in range(1, self.gun_count + 1):
            gun_id = f"{i:02d}"
            GunStatus.objects.get_or_create(
                pile=self,
                gun_id=gun_id,
                defaults={'status': '02', 'plugged_in': '01'}
            )

# models.py (新增代码)
class GunStatus(models.Model):
    STATUS_CHOICES = (
        ('00', '离线'),
        ('01', '故障'),
        ('02', '空闲'),
        ('03', '充电中'),
    )
    CONNECTION_CHOICES = (
        ('00', '未插枪'),
        ('01', '已插枪'),
    )

    pile = models.ForeignKey(Pile, on_delete=models.CASCADE, related_name='gun_statuses')
    gun_id = models.CharField(max_length=2, verbose_name='枪号')
    status = models.CharField(max_length=2, choices=STATUS_CHOICES, default='02', verbose_name='状态')
    plugged_in = models.CharField(max_length=2, choices=CONNECTION_CHOICES, default='01', verbose_name='插枪状态')

    class Meta:
        unique_together = (('pile', 'gun_id'),)
        verbose_name = '枪状态'
        verbose_name_plural = '枪状态管理'

    def __str__(self):
        return f"{self.pile.number}-{self.gun_id}"


class Order(models.Model):
    ORDER_STATUS = (
        ('pending', '待开始'),
        ('charging', '充电中'),
        ('completed', '已完成'),
        ('failed', '失败'),
    )

    order_sn = models.CharField('订单号', max_length=32, unique=True)
    station = models.ForeignKey(Station, on_delete=models.CASCADE, verbose_name='所属站点')
    pile = models.ForeignKey(Pile, on_delete=models.CASCADE, verbose_name='充电桩')
    gun_id = models.CharField('枪号', max_length=2)
    status = models.CharField('订单状态', max_length=20, choices=ORDER_STATUS, default='pending')
    start_soc = models.FloatField('起始SOC', default=0)
    end_soc = models.FloatField('结束SOC', default=0)

    energy = models.FloatField('充电度数', default=0)
    total_cost = models.FloatField('充电总金额', default=0)

    stop_reason = models.CharField('结束原因', max_length=100, blank=True, null=True)
    created_at = models.DateTimeField('添加时间', default=timezone.now)

    start_time = models.DateTimeField('开始时间', blank=True, null=True)
    end_time = models.DateTimeField('结束时间', blank=True, null=True)

    class Meta:
        verbose_name = '订单管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.order_sn